<?php
if(!defined('SYSTEM_PATH')) die('Cannot access this page directly or see your configuration file ') ;

Loader::includeClass('smarty/Smarty.class');

/**
 * Template
 * 
 * @package Cosmo
 * @author menTiss - Staff SeS
 * @copyright 2007-2008
 * @version 0.1.1
 * @access public
 **/
class Template extends Cosmo {
	public $_smarty ; // Stocke l'objet smarty
	private $_listTemplate = array() ; // stocke la liste des templates
	private $bIsDisplay = false ;
	
	/**
	 * Template::__construct()
	 * 
	 * @desc Create instance of the object
	 * 
	 * @param string $template
	 * 
	 * @return void
	 **/
	public function __construct() {
		parent::__construct() ;
		//require_once(SYSTEM_PATH.'classes/smarty/Smarty.class'.EXT) ;
		$this->_smarty = new Smarty() ;	
		$this->_smarty->caching = SMARTY_CACHE ;
		$this->_smarty->template_dir 	= TEMPLATE_PATH ;
		$this->_smarty->compile_dir 	= TEMPLATE_COMPILE_PATH ;
		$this->_smarty->assign('site_name',SITE_NAME) ;
		$this->_smarty->assign('base_url',BASE_URL) ;		
	}
	/**
	 * Template::setPath()
	 * 
	 * @desc Allow to set special path for template
	 * 
	 * @param string $type
	 * @param string $extendedPath
	 * 
	 * @return bool
	 */
	public function setPath($type, $extendedPath) {
		switch(strtolower($type)) {
			case 'template' :		
				if(is_dir(TEMPLATE_PATH.$extendedPath)) {
					$this->_smarty->template_dir = TEMPLATE_PATH.$extendedPath ;
					return true;
				}
				break;
			case 'compile' :
				if(is_dir(TEMPLATE_COMPILE_PATH.$extendedPath)) {
					$this->_smarty->compile_dir = TEMPLATE_COMPILE_PATH.$extendedPath ;
					return true;
				}
				break ;
			case 'all' :
				$change = array(false,false) ;
				if(is_dir(TEMPLATE_COMPILE_PATH.$extendedPath)) {
					$this->_smarty->compile_dir = TEMPLATE_COMPILE_PATH.$extendedPath ;
					$change[0] =  true;
				}
				if(is_dir(TEMPLATE_PATH.$extendedPath)) {
					$this->_smarty->template_dir = TEMPLATE_PATH.$extendedPath ;
					$change[1] =  true;
				}
				if($change[0] === $change[1]) {
					return $change[0];
				} 
				unset($change) ; // free memory
				break ;
		}
		return false;
	}
	/**
	 * Template::add()
	 * 
	 * @desc Add a new template
	 * 
	 * @param string $file
	 * @param boolean $stop
	 * @param string $type
	 * @param string $name
	 * 
	 * @return bool
	 **/
	public function add($file, $stop = false, $type = 'template', $name = null) {
		
		if((file_exists($this->_smarty->template_dir.$file.'.tpl') && $type == 'template') || $type == 'html') {
			if(ereg('/',$this->_smarty->template_dir)) {
				$url = explode('/',$this->_smarty->template_dir) ;
				if(isset($url[2]) && !empty($url[2])) {
					$file = $url[2].'/'.$file ;
				}
			}
			if($type == 'html' && $name != null)
				$this->_listTemplate[] = array('content' => $type, 'value' => $file, 'key' => $name) ;
			elseif($type == 'html' && $name == null)
				$this->_listTemplate[] = array('content' => $type, 'value' => $file, 'key' => $file) ;
			else
				$this->_listTemplate[] = array('content' => $type, 'value' => $file.'.tpl', 'key' => $file) ; 			
			if($stop === true) {
				if(file_exists($this->_smarty->template_dir.FOOTER_TEMPLATE.'.tpl'))
					$this->_listTemplate[] = array('content' => 'template', 'value' => FOOTER_TEMPLATE.'.tpl') ;
				$this->display($stop) ; // stop is needed, launch display method
			}
			return true ;
		} 
		$this->addLog('error','File '.$this->_smarty->template_dir.$file.'.tpl is missing') ;
		return false ;		
	} 
	/**
	 * Template::redirect()
	 * 
	 * @desc		Redirect visitor to another location of site. $where is generaly a controller
	 * 
	 * @param 	string $where
	 * 
	 * @return 	void
	 */
	public function redirect($where) {
		header('Location: '.BASE_URL.$where.'/');
		exit() ;
	}
	/**
	 * Template::remove()
	 * 
	 * @desc Remove a template from the list
	 * 
	 * @param string $file
	 * 
	 * @return bool
	 **/
	public function remove($file) {
		foreach($this->_listTemplate as $key => $value) {
			if($value['key'] == $file) {
				$this->addLog('debug','Removing template '.$file) ;
				unset($this->_listTemplate[$key]) ;
				return true ;
			}
		}
		return false ;
	}
	/**
	 * Template::remove_all()
	 * 
	 * @desc Remove all template from the template list
	 * 
	 * @return void 
	 */
	public function remove_all() {
		$this->addLog('debug','Removing all template in the list') ;
		unset($this->listTemplate) ;
		$this->_listTemplate = array() ;
	}
	/**
	 * Template::exists()
	 * 
	 * @desc Check if a template has already been added to the list
	 * 
	 * @param string $file
	 * 
	 * @return bool
	 */
	public function exists($file) {
		foreach($this->_listTemplate as $template) {
			if($template['key'] == $file) {
				return true;
			}
		}
		return false;
	}
	/**
	 * Template::display()
	 * 
	 * @desc Display all template in the list
	 * 
	 * @return void
	 **/
	public function display($stop = false) {
		$this->bIsDisplay = true ;
		foreach($this->_listTemplate as $oneTemplate) {
			if($oneTemplate['content'] == 'template') {
				$this->_smarty->display($oneTemplate['value'] ) ;
			} else {
				echo $oneTemplate['value'] ;
			} 
		}
		if($stop === true) // stop executing code
			die() ;
	}
	/**
	 * Template::register_postfilter()
	 * 
	 * @desc Allias to register_postfilter method of smarty, check if function exists
	 * 
	 * @param string $function
	 * 
	 * @return void
	 **/
	public function register_postfilter($function) {
		if(function_exists($function)) {
			$this->_smarty->register_postfilter($function) ; 
		}
	}
	/**
	 * Template::register_prefilter()
	 * 
	 * @desc Allias to register_prefilter method of smarty, check if function exists
	 *
	 * @param string $function
	 * 
	 * @return void
	 **/
	public function register_prefilter($function) {
		if(function_exists($function)) {
			$this->_smarty->register_prefilter($function)  ;
		}
	}
	/**
	 * Template::register_outputfilter()
	 * 
	 * @desc Allias to register_outputfilter method of smarty, check if function exists
	 *  
	 * @param string $function
	 * 
	 * @return void
	 **/
	public function register_outputfilter($function) {
		if(function_exists($function)) {
			$this->_smarty->register_outputfilter($function) ;
		}
	}
	/**
	 * Template::__call()
	 * 
	 * @desc Allias for all smarty function, check if a smarty method exists before. If called method does not exists, log a error message and return false 
	 * 
	 * @param string $method
	 * @param mixed $params
	 * 
	 * @return mixed
	 */
	public function __call($method, $params) {
		if(method_exists($this->_smarty,$method))
			return call_user_func_array(array($this->_smarty,$method),$params) ;
		else {
			$this->addLog('error','Request of a method which does not exists in Smarty library') ;
			return false ;
		}
	}
	public function isDisplay() {
		return $this->bIsDisplay ;
	} 
}

?>